package com.egao.api.controller.nont;


import com.egao.common.core.Constants;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.core.utils.FileUploadUtil;
import com.egao.common.core.web.BaseController;
import com.egao.common.system.entity.User;
import com.egao.common.system.service.OperRecordService;
import com.egao.orderB.entity.ApplyCustoms;
import com.egao.orderB.service.ApplyCustomsFeeService;
import com.egao.orderB.service.ApplyCustomsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * api登录相关接口
 * Created by wangfan on 2019-05-29 下午 3:43.
 * http://localhost/swagger-ui.html
 * https://francjc.com:4332/swagger-ui.html
 */
@RequestMapping("/api/nt/applyCustoms")
@RestController
@Api(value = "报关Excel导出",tags = {"报关Excel导出"})
public class ApiApplyCustomsNtController extends BaseController {

    @Autowired
    private OperRecordService operRecordService;
    @Autowired
    private ApplyCustomsService applyCustomsService;
    @Autowired
    private ApplyCustomsFeeService applyCustomsFeeService;


    @GetMapping("/exportsFinance")
    @ApiOperation(value = "应收应付分类导出财务报表",notes = "" +
        "date1: 2021-01-29\n" +
        "date2: 2021-01-30\n" +
        "customer: 客户\n" +
        "customsBroker: 报关行\n" +
//        "customer: 凯达 \n" +
        "",httpMethod = "GET")
    public void exportsFinance(PageResult2<T> page, ApplyCustoms applyCustoms, HttpServletRequest request, HttpServletResponse response) throws Exception{
        File file = new File(Constants.UPLOAD_DIR + "file/", "applyCustom.xlsx");
        if(file.exists()) {
            file.delete();
        }
        applyCustoms.setDate1(applyCustoms.getDate1().substring(0,10)+" 00:00:00");
        if(Utils.isNotEmpty(applyCustoms.getDate2())){
            applyCustoms.setDate2(applyCustoms.getDate2().substring(0,10)+" 23:59:59");
        }
        //https://hutool.cn/docs/#/poi/Excel%E7%94%9F%E6%88%90-ExcelWriter
        List<ApplyCustoms> applyCustomsList = applyCustomsService.exportList(applyCustoms, new User(1));
        if(applyCustomsList != null && applyCustomsList.size() > 0){
            ApplyCustoms applyCustoms2 = new ApplyCustoms();
            applyCustoms2.setType("应收");
            applyCustoms2.setDate1(applyCustoms.getDate1());
            applyCustoms2.setDate2(applyCustoms.getDate2());
            applyCustoms2.setKeywords(applyCustoms.getCustomer());
            List<String> feeNamesys = applyCustomsFeeService.feeNames(applyCustoms2, null, new User(1));
            applyCustoms2.setType("应付");
            List<String> feenames = new ArrayList<>();
            List<String> feeNamesyf = applyCustomsFeeService.feeNames(applyCustoms2, feenames, new User(1));
            if(feeNamesys != null && feeNamesys.size() > 0
                    && feeNamesyf != null && feeNamesyf.size() > 0
            ) {
                applyCustomsFeeService.exportsFinance(applyCustomsList,feeNamesys, feeNamesyf, applyCustoms);
                //http://localhost:9331/api/nt/applyCustoms/exportsList
                FileUploadUtil.download("finance.xlsx", response);
            } else {
                response.setContentType("text/html; charset=UTF-8");
                response.getWriter().println("没有对应的数据哦,请再次查询!");
            }
        } else {
            response.setContentType("text/html; charset=UTF-8");
            response.getWriter().println("没有对应的数据哦,请再次查询!");
        }
    }

    @GetMapping("/exportsCustomer")
    @ApiOperation(value = "应收导出客户报表",notes = "" +
        "date1: 2021-01-29\n" +
        "date2: 2021-01-30\n" +
        "customer: 客户\n" +
//        "customer: 凯达 \n" +
        "",httpMethod = "GET")
    public void exportsCustomer(PageResult2<T> page, ApplyCustoms applyCustoms, HttpServletRequest request, HttpServletResponse response) throws Exception{
        File file = new File(Constants.UPLOAD_DIR + "file/", "applyCustom.xlsx");
        if(file.exists()) {
            file.delete();
        }
        applyCustoms.setDate1(applyCustoms.getDate1().substring(0,10)+" 00:00:00");
        if(Utils.isNotEmpty(applyCustoms.getDate2())){
            applyCustoms.setDate2(applyCustoms.getDate2().substring(0,10)+" 23:59:59");
        }
        //https://hutool.cn/docs/#/poi/Excel%E7%94%9F%E6%88%90-ExcelWriter
        List<ApplyCustoms> applyCustomsList = applyCustomsService.exportList(applyCustoms, new User(1));
        if(applyCustomsList != null && applyCustomsList.size() > 0){
            ApplyCustoms applyCustoms2 = new ApplyCustoms();
            applyCustoms2.setType("应收");
            applyCustoms2.setDate1(applyCustoms.getDate1());
            applyCustoms2.setDate2(applyCustoms.getDate2());
            applyCustoms2.setCustomer(applyCustoms.getCustomer());
            List<String> feeNamesys = applyCustomsFeeService.feeNames(applyCustoms2, null, new User(1));
            applyCustoms2.setType("应付");
            List<String> feenames = new ArrayList<>();
            List<String> feeNamesyf = applyCustomsFeeService.feeNames(applyCustoms2, feenames, new User(1));
            if(feeNamesys != null && feeNamesys.size() > 0
            ) {
                applyCustomsFeeService.exportsCustomer(applyCustomsList,feeNamesys, feeNamesyf, applyCustoms);
                //http://localhost:9331/api/nt/applyCustoms/exportsList
                FileUploadUtil.download("finance.xlsx", response);
            } else {
                response.setContentType("text/html; charset=UTF-8");
                response.getWriter().println("没有对应的数据哦,请再次查询!");
            }
        } else {
            response.setContentType("text/html; charset=UTF-8");
            response.getWriter().println("没有对应的数据哦,请再次查询!");
        }
    }

    @GetMapping("/exportsApplyCustoms")
    @ApiOperation(value = "应付导出报关代理报表",notes = "" +
        "date1: 2021-01-29\n" +
        "date2: 2021-01-30\n" +
        "customer: 客户\n" +
//        "customer: 凯达 \n" +
        "",httpMethod = "GET")
    public void exportsApplyCustoms(PageResult2<T> page, ApplyCustoms applyCustoms, HttpServletRequest request, HttpServletResponse response) throws Exception{
        File file = new File(Constants.UPLOAD_DIR + "file/", "applyCustom.xlsx");
        if(file.exists()) {
            file.delete();
        }
        applyCustoms.setDate1(applyCustoms.getDate1().substring(0,10)+" 00:00:00");
        if(Utils.isNotEmpty(applyCustoms.getDate2())){
            applyCustoms.setDate2(applyCustoms.getDate2().substring(0,10)+" 23:59:59");
        }
        //https://hutool.cn/docs/#/poi/Excel%E7%94%9F%E6%88%90-ExcelWriter
        List<ApplyCustoms> applyCustomsList = applyCustomsService.exportList(applyCustoms, new User(1));
        if(applyCustomsList != null && applyCustomsList.size() > 0){
            ApplyCustoms applyCustoms2 = new ApplyCustoms();
            applyCustoms2.setType("应收");
            applyCustoms2.setDate1(applyCustoms.getDate1());
            applyCustoms2.setDate2(applyCustoms.getDate2());
            applyCustoms2.setKeywords(applyCustoms.getCustomer());
            List<String> feeNamesys = applyCustomsFeeService.feeNames(applyCustoms2, null, new User(1));
            applyCustoms2.setType("应付");
            List<String> feenames = new ArrayList<>();
            List<String> feeNamesyf = applyCustomsFeeService.feeNames(applyCustoms2, feenames, new User(1));
            if(feeNamesys != null && feeNamesys.size() > 0
                    && feeNamesyf != null && feeNamesyf.size() > 0
            ) {
                applyCustomsFeeService.exportsApplyCustoms(applyCustomsList,feeNamesys, feeNamesyf, applyCustoms);
                //http://localhost:9331/api/nt/applyCustoms/exportsList
                FileUploadUtil.download("finance.xlsx", response);
            } else {
                response.setContentType("text/html; charset=UTF-8");
                response.getWriter().println("没有对应的数据哦,请再次查询!");
            }
        } else {
            response.setContentType("text/html; charset=UTF-8");
            response.getWriter().println("没有对应的数据哦,请再次查询!");
        }
    }

}
